// +----------------------------------------------------------------------
// | Project:   Android-Commons  
// +----------------------------------------------------------------------
// | CreateTime: 15/12/14  下午6:11
// +----------------------------------------------------------------------
// | Author:     xab(xab@xabad.cn)
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
package cn.xabad.commons.converter;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * DESC   :
 * AUTHOR : Xabad
 */
public class XsonObject {

    private JSONObject jsonObject;


    public XsonObject() {
        jsonObject = new JSONObject();
    }


    public XsonObject(String res) throws JSONException {
        jsonObject = new JSONObject(res);
    }


    public boolean contians(String key, String value) {
        if (jsonObject == null) { return false; }
        if (!jsonObject.has(key)) { return false; }
        try {
            return jsonObject.getString(key)
                             .contains(value);
        } catch (JSONException e) {
            return false;
        }
    }


    public boolean equals(String key, String value) {
        if (key == null) { return false; }
        if (jsonObject == null) { return false; }
        if (!jsonObject.has(key)) { return false; }
        try {
            return jsonObject.getString(key)
                             .equals(value);
        } catch (JSONException e) {
            return false;
        }
    }


    public boolean equals(String key, boolean value) {
        if (key == null) { return false; }
        if (jsonObject == null) { return false; }
        if (!jsonObject.has(key)) { return false; }
        try {
            return jsonObject.getBoolean(key) == value;
        } catch (JSONException e) {
            return false;
        }
    }


    public boolean equals(String key, long value) {
        if (key == null) { return false; }
        if (jsonObject == null) { return false; }
        if (!jsonObject.has(key)) { return false; }
        try {
            return jsonObject.getLong(key) == value;
        } catch (JSONException e) {
            return false;
        }
    }


    public boolean equals(String key, int value) {
        if (key == null) { return false; }
        if (jsonObject == null) { return false; }
        if (!jsonObject.has(key)) { return false; }
        try {
            return jsonObject.getInt(key) == value;
        } catch (JSONException e) {
            return false;
        }
    }


    public String getString(String key) {
        if (key == null) { return null; }
        if (jsonObject == null) { return null; }
        if (!jsonObject.has(key)) { return null; }
        try {
            return jsonObject.getString(key);
        } catch (JSONException e) {
            return null;
        }
    }


    public long getLong(String key) {
        if (key == null) { return 0L; }
        if (jsonObject == null) { return 0L; }
        if (!jsonObject.has(key)) { return 0L; }
        try {
            return jsonObject.getLong(key);
        } catch (JSONException e) {
            return 0L;
        }
    }


    @Override
    public String toString() {
        if (jsonObject != null) { return jsonObject.toString(); } else {
            return null;
        }
    }
}
